package com.iad.controller.command
{
  import com.adobe.cairngorm.commands.Command;
  import com.adobe.cairngorm.control.CairngormEvent;
  import com.iad.controller.event.*;
  import com.iad.controller.manager.*;
  import com.iad.controller.service.*;
  import com.iad.model.*;
  import com.iad.model.valueobject.*;
  import com.iad.view.component.*;
  import com.iad.view.form.*;
  import com.iad.view.util.ascb.util.*;
  
  import flash.events.*;
  import flash.net.*;
  
  public class DownloadCommand implements Command
  {
    [Bindable] private var model:ModelLocatorImpl = ModelLocatorImpl.getInstance();
    private var downloadSongRef:FileReference = new FileReference();
    private var mp3Player:MP3_Player;
    
 
    public function execute(event:CairngormEvent):void {
      switch (event.type) {
        case DownloadEvent.DOWNLOAD_SONG:
          downloadSong(event as DownloadEvent);
        break;
     }
    }
    
    
    private function downloadSong(event:DownloadEvent):void {
      mp3Player = event.object as MP3_Player;
      downloadSongRef.addEventListener(Event.CANCEL, cancelHandler);
      downloadSongRef.addEventListener(Event.COMPLETE, downloadCompleteHandler);
      downloadSongRef.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
      downloadSongRef.addEventListener(Event.OPEN, openHandler);
      downloadSongRef.addEventListener(ProgressEvent.PROGRESS, downloadProgressHandler);
      downloadSongRef.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
      downloadSongRef.addEventListener(Event.SELECT, selectHandler);
      var url:String = model.sitePath + model.playlistSiteId + "/audio/" + (model.currentSong as Song).path;
      var urlReq:URLRequest = new URLRequest(url);
      try {
        downloadSongRef.download(urlReq); 
      } 
      catch (error:Error) {
        trace("Unable to download file." + error);
    }
  }
  
  
  private function cancelHandler(event:Event):void {
    trace("cancelHandler: " + event);
  }

  private function downloadCompleteHandler(event:Event):void {
    trace("downloadCompleteHandler: " + event);
    mp3Player.pBar.visible = false;
  }

  private function ioErrorHandler(event:IOErrorEvent):void {
    trace("ioErrorHandler: " + event);
  }

  private function openHandler(event:Event):void {
    trace("openHandler: " + event);
  }
  
  private function securityErrorHandler(event:SecurityErrorEvent):void {
    trace("securityErrorHandler: " + event);
  }

  private function selectHandler(event:Event):void {
    var file:FileReference = FileReference(event.target);
    trace("selectHandler: name=" + file.name);
  }
  
  private function downloadProgressHandler(event:ProgressEvent):void {
    mp3Player.pBar.visible=true;
    var bytesLoaded:Number = event.bytesLoaded;
    var bytesTotal:Number = event.bytesTotal;
    var pctLoaded:Number = Math.round(bytesLoaded / bytesTotal * 100); 
    mp3Player.pBar.setProgress(pctLoaded,100);
    trace("downLoadProgressHandler():  bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal + " pctLoaded=" + pctLoaded);
  }
    
    

     
  } 
}
    